<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\task;
use App\project;
use Redirect;
use App\Http\Requests\EditTask;
use App\Http\Requests\CreateTask;
use Auth;
class TaskController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $done = Auth::user()->tasks()->where('status',1)->paginate(1);
        $todo = Auth::user()->tasks()->where('status',0)->paginate(2);
        $projectss = project::pluck('name','id');
        //compact创建一个包含变量名和它们的值的数组
        return view('task.index',compact('projectss','todo','done'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateTask $request)
    {
        task::create([
                'title'=>$request->name,
                'project_id'=>$request->projectid,
            ]);

        return Redirect::back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    public function check($id){
        $task = task::findOrFail($id);
        $task->status = 1;
        $task->save();
        return Redirect::back();
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(EditTask $request, $id)
    {
        $task = task::findOrFail($id);
        $task->title = $request->title;
        $task->project_id = $request->projectss;
        $task->save();

        return Redirect::back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        task::find($id)->delete();
        return Redirect::back();
    }
}
